//
//  DJMessageSystemVC.m
//  daojiVideo
//
//  Created by adam on 2024/5/21.
//

#import "DJMessageSystemVC.h"
#import "DJMessageSystemCell.h"

@interface DJMessageSystemVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *systemTableView;
@property (strong, nonatomic) NSMutableArray *listArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) id lastId;
@property (strong, nonatomic) PublicView *nullView;

@end

@implementation DJMessageSystemVC
- (NSMutableArray *)listArray{
    if(_listArray == nil){
        _listArray = [NSMutableArray new];
    }
    return _listArray;
}
- (PublicView *)nullView{
    if(_nullView == nil){
        _nullView = [[PublicView alloc]initWithFrame:CGRectMake(0, (KScreen_H-KNav_H-200)/2, KScreen_W, 200)];
        if(self.m_type == 6){
            _nullView.publicLabel.text = @"还没有系统消息";
        }else if (self.m_type == 5){
            _nullView.publicLabel.text = @"还没有平台公告";
        }else if (self.m_type == 4){
            _nullView.publicLabel.text = @"还没有钱包消息";
        }
//        else if (self.m_type == 4){
//            _nullView.publicLabel.text = @"还没有金币钱包消息";
//        }
        
        _nullView.hidden = YES;
        _nullView.userInteractionEnabled = NO;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_nullView];
    }
    return _nullView;
}
-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = viewback_black;
    if(self.m_type == 6){
        self.navigationItem.title = @"系统消息";
    }else if (self.m_type == 5){
        self.navigationItem.title = @"平台公告";
    }else if (self.m_type == 4){
        self.navigationItem.title = @"收益钱包";
    }
//    else if (self.m_type == 4){
//        self.navigationItem.title = @"金币钱包";
//    }
    [self.systemTableView registerNib:[UINib nibWithNibName:@"DJMessageSystemCell" bundle:nil] forCellReuseIdentifier:@"DJMessageSystemCell"];
    self.systemTableView.delegate = self;
    self.systemTableView.dataSource = self;
    KWeakSelf
    self.systemTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf requestData:NO];
    }];
    self.systemTableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //下拉刷新要做的操作.
        [weakSelf.systemTableView.mj_footer resetNoMoreData];
        [weakSelf.listArray removeAllObjects];
        [weakSelf.systemTableView reloadData];
        NSLog(@"下拉");
        weakSelf.page = 0;
        [weakSelf requestData:NO];
    }];
    [self requestData:YES];
}
- (void)requestData:(BOOL)show {
    KMutableDic(sendDic)
    [sendDic setObject:@(self.page) forKey:@"page"];
    if(self.m_type == 6){
        [sendDic setObject:@"xt" forKey:@"news_type"];
    }else if (self.m_type == 5){
        [sendDic setObject:@"team" forKey:@"news_type"];
    }else if (self.m_type == 4){
        [sendDic setObject:@"qb" forKey:@"news_type"];
    }
//    else if (self.m_type == 4){
//        [sendDic setObject:@"qb" forKey:@"news_type"];
//    }
    
    if(self.page == 0){
        [sendDic setObject:@(0) forKey:@"last_id"];
    }else{
        [sendDic setObject:self.lastId forKey:@"last_id"];
    }
    KWeakSelf
    [YBNetBase newsTypeListRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            if(weakSelf.page == 0){
                [weakSelf.listArray removeAllObjects];
            }
            weakSelf.lastId = data[@"last_id"];
            [weakSelf.listArray addObjectsFromArray:data[@"list"]];
            [weakSelf.systemTableView reloadData];
            [weakSelf tableEndFiresh:data[@"list"]];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
-(void)showTextViewAddShowViewTo2{
    UIView * showTextViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 360)];
    showTextViewViewTo1.backgroundColor = [UIColor whiteColor];
    showTextViewViewTo1.layer.cornerRadius = 8;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewViewTo1];
    
    UIImageView * showTextViewimgTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(157.5), K_AUTOLAYOUT_W(260), K_AUTOLAYOUT_W(60), K_AUTOLAYOUT_W(60))];
    showTextViewimgTo2.layer.cornerRadius = K_AUTOLAYOUT_W(30);
    showTextViewimgTo2.clipsToBounds = YES;
    showTextViewimgTo2.contentMode = UIViewContentModeScaleAspectFill;
    showTextViewimgTo2.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo2];
    
    UILabel *  showTextViewimgTo3 = [[UILabel alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(15), K_AUTOLAYOUT_W(35), K_AUTOLAYOUT_W(345), K_AUTOLAYOUT_W(25))];
    showTextViewimgTo3.font = FontBold(18);
    showTextViewimgTo3.textColor = ThreeColor;
    showTextViewimgTo3.textAlignment = NSTextAlignmentCenter;
    showTextViewimgTo3.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo3];
}
- (NSString *)showTextViewGetNowTimerTo3{
    NSDateFormatter *showTextViewGetNowFa = [[NSDateFormatter alloc] init];
    [showTextViewGetNowFa setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [showTextViewGetNowFa stringFromDate:[NSDate date]];
}

- (void)tableEndFiresh:(NSArray *)list{
    if([self.systemTableView.mj_header isRefreshing]){
        [self.systemTableView.mj_header endRefreshing];
    }else if ([self.systemTableView.mj_footer isRefreshing]){
        if([list count] == 0){
            [self.systemTableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.systemTableView.mj_footer endRefreshing];
        }
    }else{
        if(self.listArray.count == 0){
            self.nullView.hidden = NO;
            self.systemTableView.mj_footer.hidden = YES;
        }else{
            self.nullView.hidden = YES;
            self.systemTableView.mj_footer.hidden = NO;
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DJMessageSystemCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DJMessageSystemCell"];
    [cell showMessageCell:self.listArray[indexPath.row]];
    return cell;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    //1.删除按钮
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        //1.更新数据源
        [self deleteRow:indexPath];
        //2.更新UI
    }];
      deleteAction.backgroundColor = MainColor;
    return @[deleteAction];//返回按钮数组
}
/**
 *  只要实现了这个方法，左滑出现Delete按钮的功能就有了
 *  点击了“左滑出现的Delete按钮”会调用这个方法
 */
//IOS9前自定义左滑多个按钮需实现此方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self deleteRow:indexPath];
}

/**
 *  修改Delete按钮文字为“删除”
 */
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
-(void)deleteRow:(NSIndexPath *)indexPath{
    [SVProgressHUDTool showWithStatus:nil];
    KMutableDic(sendDic)
    if(self.m_type == 6){
        [sendDic setObject:@"xt" forKey:@"type"];
    }else if (self.m_type == 5){
        [sendDic setObject:@"team" forKey:@"type"];
    }else if (self.m_type == 4){
        [sendDic setObject:@"qb" forKey:@"type"];
    }
    [sendDic setValue:self.listArray[indexPath.row][@"id"] forKey:@"ids"];
    KWeakSelf
    [YBNetBase delNewsRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        [SVProgressHUDTool dismiss];
        if(code == 200){
            [weakSelf.listArray removeObjectAtIndex:indexPath.row];
            [YBMsgPop showPop:@"消息删除成功"];
            [weakSelf.systemTableView reloadData];
        }else{
            [YBMsgPop showPop:msg];
        }
        
    }];
}
@end
